#include <stdlib.h>
#include <stdio.h>

#include "fsl_debug_console.h"

#include "ring_buf.h"
#include "audio.h"

int32_t ring_buf_init(RING_BUF *ring_buf_str)
{
	ring_buf_str->buffer = (int16_t *)malloc(sizeof(int16_t) * \
		NUM_RING_BUF_FRAMES * NUM_FRAM_SAMPLE);		/* NUM_RING_BUF_FRAMES 为12时，缓冲器大小为15k */
	if (ring_buf_str->buffer == NULL)
	{
		PRINTF("The ring bug malloc failed\r\n");
		return -1;
	} else {
		PRINTF("The ring buf malloc success\r\n");
	}
	memset(ring_buf_str->buffer, 0, sizeof(int16_t) *\
		NUM_RING_BUF_FRAMES * NUM_FRAM_SAMPLE);
	ring_buf_str->start = ring_buf_str->buffer;
	ring_buf_str->len_start_to_end = NUM_RING_BUF_FRAMES;
	return 0;
}

void ring_buf_input(RING_BUF *ring_buf_str, int16_t *data_buf)
{
	if (data_buf == NULL)
	{
		return -1;
	}
	/* 将数据copy到头部指针处 */
	memcpy((void *)ring_buf_str->start, (void *)data_buf, sizeof(int16_t) * \
		NUM_FRAM_SAMPLE);
	/* 如果start 指针到尾部长度为1，则开启新循环 */
	if (ring_buf_str->len_start_to_end == 1)
	{
		ring_buf_str->start = ring_buf_str->buffer;
		ring_buf_str->len_start_to_end = NUM_RING_BUF_FRAMES;
	} else {
		ring_buf_str->start = ring_buf_str->start + NUM_FRAM_SAMPLE;
		ring_buf_str->len_start_to_end = \
			ring_buf_str->len_start_to_end - 1;
	}
}

void ring_buf_output(RING_BUF *ring_buf_str, int16_t *audio_buf)
{
	int16_t *data_buf_tmp = audio_buf;
	/* 先将start指针到尾部数据copy到目的地址 */	
	memcpy(data_buf_tmp, ring_buf_str->start, sizeof(int16_t) * \
		NUM_FRAM_SAMPLE * ring_buf_str->len_start_to_end);
	data_buf_tmp += (NUM_FRAM_SAMPLE * ring_buf_str->len_start_to_end);
	memcpy(data_buf_tmp, ring_buf_str->buffer, sizeof(int16_t) * \
		NUM_FRAM_SAMPLE * (NUM_RING_BUF_FRAMES - ring_buf_str->len_start_to_end));
}
/* 环形缓冲测试用代码 */

// int32_t ring_buf_init(RING_BUF *ring_buf_str)
// {
// 	ring_buf_str->buffer = malloc(sizeof(int16_t) * \
// 		NUM_RING_BUF_FRAMES);
// 	if (ring_buf_str->buffer == NULL)
// 	{
// 		printf("The ring bug malloc failed\r\n");
// 		return -1;
// 	}
// 	memset(ring_buf_str->buffer, 0, sizeof(int16_t) *\
// 		NUM_RING_BUF_FRAMES);
// 	ring_buf_str->start = ring_buf_str->buffer;
// 	ring_buf_str->len_start_to_end = NUM_RING_BUF_FRAMES;
// 	return 0;
// }

// int32_t ring_buf_input(RING_BUF *ring_buf_str, int16_t *data_buf)
// {
// 	if (data_buf == NULL)
// 	{
// 		return -1;
// 	}
// 	memcpy(ring_buf_str->start, data_buf, sizeof(int16_t));
// 	if (ring_buf_str->len_start_to_end == 1)
// 	{
// 		ring_buf_str->start = ring_buf_str->buffer;
// 		ring_buf_str->len_start_to_end = NUM_RING_BUF_FRAMES;
// 	} else {
// 		ring_buf_str->start = ring_buf_str->start + 1;
// 		ring_buf_str->len_start_to_end = ring_buf_str->len_start_to_end - 1;
// 	}
// 	return 0;
// }

// void ring_buf_output(RING_BUF *ring_buf_str, int16_t *data_buf)
// {
// 	// int16_t *data_buf_tmp = data_buf;	
// 	// memcpy(data_buf_tmp, ring_buf_str->start, sizeof(int16_t) * \
// 	// 	NUM_FRAM_SAMPLE * ring_buf_str->len_start_to_end);
// 	// data_buf_tmp += ( sizeof(int16_t) * \
// 	// 	NUM_FRAM_SAMPLE * ring_buf_str->len_start_to_end);
// 	// memcpy(data_buf_tmp, ring_buf_str->buffer * sizeof(int16_t) * \
// 	// 	NUM_FRAM_SAMPLE * (ring_buf_str->len_buf - ring_buf_str->len_start_to_end));
// 	uint32_t i;
// 	for (i = 0; i < ring_buf_str->len_start_to_end; i ++)
// 	{
// 		printf("%d\r\n", *(ring_buf_str->start + i));
// 	}
// 	for (i = 0; i < (NUM_RING_BUF_FRAMES - ring_buf_str->len_start_to_end);i++)
// 	{
// 		printf("%d\r\n", *(ring_buf_str->buffer + i));
// 	}
// }
